<?php
/**
 * Created by PhpStorm.
 * User: fuyu
 * Date: 2018/6/21
 * Time: 下午2:55
 */

namespace App\Http\Controllers;


use App\Models\Product;
use App\Models\ProductOrderSource;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function add(Request $request)
    {
        $this->validate($request,[
            'name' => 'required|max:100',
            'code' => 'required|max:100',
            'weight' => 'required|numeric'
        ]);
        $product = Product::create($request->post());
        return jsonSuccess('添加成功',$product);
    }

    public function modify(Request $request)
    {
        $this->validate($request,[
            'id' => 'required',
            'name' => 'required|max:100',
            'code' => 'required|max:100',
            'weight' => 'required|numeric'
        ]);
        $product = Product::find($request->post('id'));
        if (empty($product)) {
            return jsonFail('找不到该产品');
        }
        $product->fill($request->post());
        $product->save();
        return jsonSuccess('修改成功',$product);
    }

    public function delete(Request $request)
    {
        $this->validate($request,[
            'id' => 'required'
        ]);
        $product = Product::find($request->post('id'));
        if (empty($product)) {
            return jsonFail('找不到该产品');
        }
        $product->delete();
        return jsonSuccess('删除成功');
    }

    public function info(Request $request)
    {
        $this->validate($request,[
            'id' => 'required'
        ]);
        $product = Product::find($request->get('id'));
        if (empty($product)) {
            return jsonFail('找不到该产品');
        }
        return jsonSuccess('获取成功',$product);
    }

    public function list(Request $request)
    {
        $pageSize = $request->input('page_size',20);
        $products = Product::paginate($pageSize);
        $pagination = ['current_page'=>$products->currentPage(),'last_page'=>$products->lastPage()];
        return jsonSuccess('获取成功',['list'=>$products->items(),'pagination'=>$pagination]);
    }

    /**
     * 添加产品的源信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function addOrderSource (Request $request)
    {
        $this->validate($request,[
            'product' => 'required|integer|exists:products,id',
            'order_source' => 'required|integer|exists:order_source,id',
            'source_name' => 'required|max:100',
            'source_code' => 'required',
            'source_quantity' => 'integer'
        ]);
        $productId = $request->post('product');
        $orderSource = $request->post('order_source');
        $sourceCode = $request->post('source_code');
        //查找是否有记录
        $record = ProductOrderSource::where(['product'=>$productId,'order_source'=>$orderSource,'source_code'=>$sourceCode])->first();
        if (empty($record)) {
            //新建一条记录
            $record = ProductOrderSource::create($request->post());
        } else {
            return jsonFail('该商品来源关系已经添加');
        }
        return jsonSuccess('编辑商品源操作成功',$record);
    }

    /**
     * 编辑产品的源信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function modifyOrderSource (Request $request)
    {
        $this->validate($request,[
            'id' => 'required|integer',
            'product' => 'required|integer|exists:products,id',
            'order_source' => 'required|integer|exists:order_source,id',
            'source_name' => 'required|max:100',
            'source_code' => 'required',
            'source_quantity' => 'integer'
        ]);
        $id = $request->post('id');
        $productId = $request->post('product');
        $orderSource = $request->post('order_source');
        $sourceCode = $request->post('source_code');
        $record = ProductOrderSource::find($id);
        if (empty($record)) {
            return jsonFail('查无此记录');
        } else {
            //查找修改后是否影响了其他记录
            $other = ProductOrderSource::where(['product'=>$productId,'order_source'=>$orderSource,'source_code'=>$sourceCode])->first();
            if (!empty($other) && $other->id != $id) {
                return jsonFail('修改失败，已经存在了相同的产品来源关系');
            }
            $record->fill($request->post());
            $record->save();
        }
        return jsonSuccess('编辑商品源操作成功',$record);
    }

    public function orderSourceList (Request $request)
    {
        $this->validate($request,[
            'product' => 'required|integer',
        ]);
        $pageSize = $request->input('page_size',20);
        $products = ProductOrderSource::where(['product'=>$request->get('product')])->paginate($pageSize);
        $pagination = ['current_page'=>$products->currentPage(),'last_page'=>$products->lastPage()];
        $items = [];
        foreach ($products->items() as $item) {
            $items[] = [
                'id' => $item->id,
                'source_code' => $item->source_code,
                'source_name' => $item->source_name,
                'source_quantity' => $item->source_quantity,
                'product_name' => $item->productObject->name,
                'product_code' => $item->productObject->code,
                'order_source_name' => $item->orderSourceObject->name,
                'gmt_create' => (string)$item->gmt_create,
                'gmt_modified' => (string)$item->gmt_modified,
            ];
        }
        return jsonSuccess('获取成功',['list'=>$items,'pagination'=>$pagination]);
    }

}